package me.w70.bot.wrappers;

import java.awt.Point;

import me.w70.bot.Bot;
import me.w70.bot.methods.Calc;
import me.w70.hooks.Tile;

public class RSCharacter {

	protected me.w70.hooks.Character c;

	public RSCharacter(final me.w70.hooks.Character c) {
		this.c = c;
	}

	@Override
	public boolean equals(final Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj instanceof RSCharacter) {
			final RSCharacter cha = (RSCharacter) obj;
			return cha.c == c;
		}
		return false;
	}

	public int getAnimation() {
		return c.getAnimation();
	}

	public int getHeight() {
		return c.getHeight();
	}

	/**
	 * Returns the % of HP. Returns 100 if not in combat.
	 */
	public int getHPPercent() {
		return 0;
		// return isInCombat() ? c.getHPRatio() * 100 / 255 : 100;
	}

	public RSCharacter getInteracting() {
		final int interact = c.getInteracting();
		if (interact == -1) {
			return null;
		}
		if (interact < 32767) {
			return new RSNPC(Bot.methods.getClient().getNPCs()[interact]);
		} else if (interact >= 32767) {
			int index = interact - 32767;
			if (index == Bot.methods.getClient().getSelfInteracting()) {
				index = 2047;
			}
			return new RSPlayer(Bot.methods.getClient().getPlayers()[index]);
		}
		return null;
	}

	public Tile getLocation() {
		if (c == null) {
			return new Tile(-1, -1);
		}
		final int x = Bot.methods.getClient().getBaseX() + (c.getPosX() >> 7);
		final int y = Bot.methods.getClient().getBaseY() + (c.getPosY() >> 7);
		return new Tile(x, y);
	}

	public String getMessage() {
		return c.getMessage();
	}

	/**
	 * Get's the minimap location, of the character. Note: This does work when
	 * it's walking!
	 * 
	 * @return
	 */
	public Point getMinimapLocation() {
		final int cX = Bot.methods.getClient().getBaseX()
				+ (c.getPosX() / 32 - 2) / 4;
		final int cY = Bot.methods.getClient().getBaseY()
				+ (c.getPosY() / 32 - 2) / 4;
		return Calc.worldToMinimap(cX, cY);
	}

	public Point getScreenLocation() {
		// RSTile loc = getLocation();
		// return Calc.tileToScreen(loc.getX(), loc.getY(),
		// 0.5, 0.9, (c.getHeight() / 2));
		return Calc.worldToScreen(c.getPosX(), c.getPosY(),
				-(c.getHeight() / 2));
	}

	@Override
	public int hashCode() {
		return System.identityHashCode(c);
	}

	public boolean isInCombat() {
		return Bot.methods.getClient().getLoopCycle() < c.getLoopCycleStatus();
	}

	public boolean isInteractingWithLocalPlayer() {
		return c.getInteracting() - 32768 == Bot.methods.getClient()
				.getSelfInteracting();
	}

	public boolean isMoving() {
		return c.getMotion() != 0;
	}

	public boolean isValid() {
		return c != null;
	}

	@Override
	public String toString() {
		final RSCharacter inter = getInteracting();
		return "[anim="
				+ getAnimation()
				+ ",msg="
				+ getMessage()
				+ ",interact="
				+ (inter == null ? "null" : inter.isValid() ? inter
						.getMessage() : "Invalid") + "]";
	}
}
